package vn.edu.saigontech.ncms.web.student;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.model.result.MoveUpTest;
import vn.edu.saigontech.ncms.service.LevelService;
import vn.edu.saigontech.ncms.service.MoveUpTestService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.service.StudentTypeService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class MoveUpTestAction extends BaseAction {
	private Long id;
	private String date;
	private String fromTestDate;
	private String toTestDate;
	private String fromConsultedDate;
	private String toConsultedDate;
	private Long typeId;
	private Double writtenScore;
	private Double speakingScore;
	private Double listeningScore;
	private Double grammarScore;
	private Double readingScore;
	private Long studentId;
	private String comment;
	private String firstName;
	private String lastName;
	private Long levelId;
	private Long programId;
	private String receiptNo;
	private String registerDate;
	private Double fee;
	private Long cashierId;
	private String consultedDate;
	private Long consultantId;
	private boolean active;

	public String showAdd() {
		Student student = StudentService.INSTANCE.findById(this.studentId);
		if (student.getProgram() == null) {
			REQUEST().setAttribute("programs",
					ProgramService.INSTANCE.findAllAreActive());
		} else {
			REQUEST().setAttribute("programs",
					Collections.singletonList(student.getProgram()));
		}
		REQUEST().setAttribute("student", student);
		REQUEST().setAttribute("cashiers",
				UserService.INSTANCE.findByRole("Consulting"));
		return SUCCESS;
	}

	public String add() {
		MoveUpTest moveUpTest = new MoveUpTest();
		Student student = StudentService.INSTANCE.findById(this.studentId);
		moveUpTest.setStudent(student);
		moveUpTest.setLevel(LevelService.INSTANCE.findById(this.levelId));
		moveUpTest.setDate(DateUtil.fromString(this.date, this
				.i18n("format.date")));
		moveUpTest.setWrittenScore(this.writtenScore);
		moveUpTest.setSpeakingScore(this.speakingScore);
		moveUpTest.setListeningScore(this.listeningScore);
		moveUpTest.setReadingScore(this.readingScore);
		moveUpTest.setGrammarScore(this.grammarScore);
		moveUpTest.setReceiptNo(this.receiptNo);
		if (Checker.isDate(this.registerDate, this.i18n("format.date"))) {
			moveUpTest.setRegisterDate(DateUtil.fromString(this.registerDate,
					this.i18n("format.date")));
		}
		moveUpTest.setFee(this.fee);
		moveUpTest.setCashier(UserService.INSTANCE.findById(this.cashierId));
		MoveUpTestService.INSTANCE.persist(moveUpTest);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.moveUp.add", student.getFullName());
		return SUCCESS;
	}

	public String list() {
		Student student = StudentService.INSTANCE.findById(this.studentId);
		Set<MoveUpTest> tests = student.getMoveUpTests();
		if (tests.isEmpty())
			return ADD;
		REQUEST().setAttribute("student", student);
		return SUCCESS;
	}
	
	public String info() {
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("moveUpTest", MoveUpTestService.INSTANCE.findById(this.id));
		REQUEST().setAttribute("cashiers", UserService.INSTANCE.findAllByRole("Consulting"));
		return SUCCESS;
	}
	
	public String edit() {
		MoveUpTest moveUpTest = MoveUpTestService.INSTANCE.findById(this.id);
		moveUpTest.setLevel(LevelService.INSTANCE.findById(this.levelId));
		moveUpTest.setDate(DateUtil.fromString(this.date, this.i18n("format.date")));
		moveUpTest.setListeningScore(this.listeningScore);
		moveUpTest.setSpeakingScore(this.speakingScore);
		moveUpTest.setGrammarScore(this.grammarScore);
		moveUpTest.setReadingScore(this.readingScore);
		moveUpTest.setWrittenScore(this.writtenScore);
		moveUpTest.setReceiptNo(this.receiptNo);
		if(Checker.isDate(this.registerDate, this.i18n("format.date"))) {
			moveUpTest.setRegisterDate(DateUtil.fromString(this.registerDate, this.i18n("format.date")));
		}
		moveUpTest.setFee(this.fee);
		moveUpTest.setCashier(UserService.INSTANCE.findById(this.cashierId));
		moveUpTest.setActive(this.active);
		moveUpTest.setComment(this.comment);
		moveUpTest = MoveUpTestService.INSTANCE.persist(moveUpTest);
		this.studentId = moveUpTest.getStudent().getId();
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.moveUp.edit", moveUpTest.getStudent().getFullName());
		return SUCCESS;
	}
	
	public String delete() {
		MoveUpTest moveUp = MoveUpTestService.INSTANCE.findById(this.id);
		this.studentId = moveUp.getStudent().getId();
		String studentName = moveUp.getStudent().getFullName();
		if(MoveUpTestService.INSTANCE.deleteById(this.id)) {
			this.addLoggingDetail(BaseAction.DELETE, "logging.moveUp.delete", studentName);
			return SUCCESS;
		}
		this.setKeyMessage("confirm.deleteUnsuccessfully", "moveUpTest");		
		return ERROR;
	}
	
	public String showSearch() {
		REQUEST().setAttribute("types", StudentTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findAllByRole("Consulting"));
		return SUCCESS;
	}
	
	public String search() {
		MoveUpTest moveUpTest = MoveUpTestService.INSTANCE.createBlankMoveUpTest();
		moveUpTest.getStudent().setFirstName(this.firstName);
		moveUpTest.getStudent().setLastName(this.lastName);
		moveUpTest.getStudent().getType().setId(this.typeId);
		moveUpTest.getStudent().setFromConsultedDate(DateUtil.fromString(this.fromConsultedDate, this.i18n("format.date")));
		moveUpTest.getStudent().setToConsultedDate(DateUtil.fromString(this.toConsultedDate, this.i18n("format.date")));
		moveUpTest.getStudent().getProgram().setId(this.programId);
		moveUpTest.getLevel().setId(this.levelId);
		moveUpTest.setDate(DateUtil.fromString(this.date, this.i18n("format.date")));
		moveUpTest.setFromTestDate(DateUtil.fromString(this.fromTestDate, this.i18n("format.date")));
		moveUpTest.setToTestDate(DateUtil.fromString(this.toTestDate, this.i18n("format.date")));
		moveUpTest.getStudent().setConsultedDate(DateUtil.fromString(this.consultedDate, this.i18n("format.date")));
		moveUpTest.getStudent().getConsultant().setId(this.consultantId);
		
		SESSION().setAttribute("searchMoveUpTest", moveUpTest);
		this.addLoggingDetail(BaseAction.SEARCH, "logging.moveUp.search");
		return SUCCESS;
	}
	
	public String showCacheSearchResult() {
		MoveUpTest moveUpTest = (MoveUpTest) SESSION().getAttribute("searchMoveUpTest");
		if(Checker.isNull(moveUpTest))
			moveUpTest = MoveUpTestService.INSTANCE.createBlankMoveUpTest();
		Collection<MoveUpTest> moveUpTests = MoveUpTestService.INSTANCE.find(moveUpTest);
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAll());
		REQUEST().setAttribute("moveUpTests", moveUpTests);
		return SUCCESS;
	}
	
	public String showDetail() {
		REQUEST().setAttribute("moveUpTest", MoveUpTestService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String showReport() {
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getFromTestDate() {
		return fromTestDate;
	}

	public void setFromTestDate(String fromTestDate) {
		this.fromTestDate = fromTestDate;
	}

	public String getToTestDate() {
		return toTestDate;
	}

	public void setToTestDate(String toTestDate) {
		this.toTestDate = toTestDate;
	}

	public String getFromConsultedDate() {
		return fromConsultedDate;
	}

	public void setFromConsultedDate(String fromConsultedDate) {
		this.fromConsultedDate = fromConsultedDate;
	}

	public String getToConsultedDate() {
		return toConsultedDate;
	}

	public void setToConsultedDate(String toConsultedDate) {
		this.toConsultedDate = toConsultedDate;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Double getWrittenScore() {
		return writtenScore;
	}

	public void setWrittenScore(Double writtenScore) {
		this.writtenScore = writtenScore;
	}

	public Double getSpeakingScore() {
		return speakingScore;
	}

	public void setSpeakingScore(Double speakingScore) {
		this.speakingScore = speakingScore;
	}

	public Double getListeningScore() {
		return listeningScore;
	}

	public void setListeningScore(Double listeningScore) {
		this.listeningScore = listeningScore;
	}

	public Double getGrammarScore() {
		return grammarScore;
	}

	public void setGrammarScore(Double grammarScore) {
		this.grammarScore = grammarScore;
	}

	public Double getReadingScore() {
		return readingScore;
	}

	public void setReadingScore(Double readingScore) {
		this.readingScore = readingScore;
	}

	public Long getStudentId() {
		return studentId;
	}

	public void setStudentId(Long studentId) {
		this.studentId = studentId;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Long getLevelId() {
		return levelId;
	}

	public void setLevelId(Long levelId) {
		this.levelId = levelId;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public String getReceiptNo() {
		return receiptNo;
	}

	public void setReceiptNo(String receiptNo) {
		this.receiptNo = receiptNo;
	}

	public String getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(String registerDate) {
		this.registerDate = registerDate;
	}

	public Double getFee() {
		return fee;
	}

	public void setFee(Double fee) {
		this.fee = fee;
	}

	public Long getCashierId() {
		return cashierId;
	}

	public void setCashierId(Long cashierId) {
		this.cashierId = cashierId;
	}

	public String getConsultedDate() {
		return consultedDate;
	}

	public void setConsultedDate(String consultedDate) {
		this.consultedDate = consultedDate;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

}
